package com.example.demo.listener;

import com.example.demo.config.Constants;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 请求应答模式监听
 *
 * @param
 * @return
 * @date 2024/11/4 15:07
 * @author lililiang
 */
@Slf4j
@Component
public class RPCListener {
    @Resource
    private RabbitTemplate rabbitTemplate;


    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(value = Constants.QUEUE_RPC_SERVER))
    public void onMessage1(Message message, Channel channel) throws IOException {
        String strReceive = new String(message.getBody(), Charset.defaultCharset());
        log.info("queue-rpc-server 监听到数据:" + strReceive);
        // 这里写业务处理
        rabbitTemplate.convertAndSend(Constants.QUEUE_RPC_CLIENT, strReceive);
    }

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(value = Constants.QUEUE_RPC_CLIENT))
    public void onMessage2(Message message, Channel channel) throws IOException {
        String strReceive = new String(message.getBody(), Charset.defaultCharset());
        log.info("queue-prc-client 监听到数据:" + strReceive);
        // 这里根据id，进行业务处理
    }
}
